"use client"

import { useState } from "react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { useLanguage } from "@/context/language-context"

export function DeepSeekTranslator() {
  const { dictionary } = useLanguage()
  const [inputText, setInputText] = useState("")
  const [translatedText, setTranslatedText] = useState("")
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)

  const handleTranslate = async () => {
    if (!inputText.trim()) {
      setError("Please enter text to translate.")
      return
    }
    setLoading(true)
    setError(null)
    setTranslatedText("")

    try {
      const response = await fetch("/api/translate", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({ text: inputText, targetLanguage: "zh" }), // Always translate English to Chinese for this demo
      })

      if (!response.ok) {
        const errorData = await response.json()
        throw new Error(errorData.error || "Translation failed.")
      }

      const data = await response.json()
      setTranslatedText(data.translatedText)
    } catch (err: any) {
      setError(err.message || "An unexpected error occurred during translation.")
      console.error("Translation error:", err)
    } finally {
      setLoading(false)
    }
  }

  return (
    <section className="py-12 md:py-20 bg-white dark:bg-gray-800">
      <div className="container mx-auto px-4 md:px-6">
        <Card className="max-w-3xl mx-auto shadow-lg">
          <CardHeader>
            <CardTitle className="text-2xl md:text-3xl text-center text-gray-900 dark:text-gray-50">
              {dictionary.deepseekDemo}
            </CardTitle>
          </CardHeader>
          <CardContent className="space-y-6">
            <div>
              <label htmlFor="inputText" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                {dictionary.enterText}
              </label>
              <Input
                id="inputText"
                type="text"
                placeholder="Hello, this is a beautiful diamond ring."
                value={inputText}
                onChange={(e) => setInputText(e.target.value)}
                className="w-full p-3 border rounded-md focus:ring-2 focus:ring-purple-500 dark:bg-gray-700 dark:border-gray-600 dark:text-gray-50"
              />
            </div>
            <Button
              onClick={handleTranslate}
              disabled={loading}
              className="w-full bg-black text-white hover:bg-gray-800 dark:bg-white dark:text-black dark:hover:bg-gray-200"
            >
              {loading ? "Translating..." : dictionary.translate}
            </Button>
            {error && <p className="text-red-500 text-sm text-center">{error}</p>}
            {translatedText && (
              <div className="mt-4 p-4 bg-gray-100 dark:bg-gray-700 rounded-md border border-gray-200 dark:border-gray-600">
                <h4 className="font-semibold text-gray-800 dark:text-gray-200 mb-2">{dictionary.translationResult}</h4>
                <p className="text-gray-700 dark:text-gray-300">{translatedText}</p>
              </div>
            )}
          </CardContent>
        </Card>
      </div>
    </section>
  )
}
